package gamagora.box2d
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2ContactListener;
	import Box2D.Dynamics.b2World;

	public class OSPhysicWorld
	{
		private static var _world:b2World;
		private static var _ratio:Number;
		private static var _delta:Number;
		private static var _velocity:Number;
		private static var _position:Number;
		
		public function OSPhysicWorld()
		{
		}
		
		public static function create(ratio:Number, delta:Number, velocity:int = 10, position:int = 10):void
		{
			_world = new b2World(new b2Vec2(0, 9.8), true);
			_ratio = ratio;
			_delta = delta;
			_velocity = velocity;
			_position = position;
		}
		
		public static function getInstance():b2World
		{
			if(_world == null)
			{
				_world = new b2World(new b2Vec2(0, 9.8), true);
			}
			return _world;
		}
		
		public static function add(def:b2BodyDef):b2Body
		{
			return getInstance().CreateBody(def);
		}
		
		public static function addContactListener(listener:b2ContactListener):void
		{
			getInstance().SetContactListener(listener);
		}
		
		public static function destroy(obj:b2Body):void
		{
			getInstance().DestroyBody(obj);
		}
		
		public static function update():void
		{
			getInstance().Step(_delta, _velocity, _position);
			getInstance().ClearForces();
		}
		
		public static function getNbObjects():uint
		{
			return getInstance().GetBodyCount() - 1;
		}
		
		//Getters Setters
		
		public static function get ratio():Number
		{
			return _ratio;
		}

		public static function get delta():Number
		{
			return _delta;
		}

	}
}